/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 2/09/2011, 07:10:33 PM
 */

package view;

import controller.CalculatorController;
import javax.swing.JOptionPane;

/**
 *
 * @author Natalia
 */
@SuppressWarnings("serial")
public class PrincipalCalculadora extends javax.swing.JFrame {

    private CalculatorController calculatorController = new  CalculatorController();

    /** Creates new form Principal */
    public PrincipalCalculadora() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        inputFTF = new javax.swing.JFormattedTextField();
        addB = new javax.swing.JButton();
        sustractB = new javax.swing.JButton();
        resultB = new javax.swing.JButton();
        multiplyB = new javax.swing.JButton();
        divideB = new javax.swing.JButton();
        expB = new javax.swing.JButton();
        logB = new javax.swing.JButton();
        cosB = new javax.swing.JButton();
        sinB = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CALCULADORA");

        inputFTF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));

        addB.setText("+");
        addB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBActionPerformed(evt);
            }
        });

        sustractB.setText("-");
        sustractB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sustractBActionPerformed(evt);
            }
        });

        resultB.setText("=");
        resultB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resultBActionPerformed(evt);
            }
        });

        multiplyB.setText("*");
        multiplyB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multiplyBActionPerformed(evt);
            }
        });

        divideB.setText("/");
        divideB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                divideBActionPerformed(evt);
            }
        });

        expB.setText("exp");
        expB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                expBActionPerformed(evt);
            }
        });

        logB.setText("log");
        logB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logBActionPerformed(evt);
            }
        });

        cosB.setText("cos");
        cosB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cosBActionPerformed(evt);
            }
        });

        sinB.setText("sin");
        sinB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sinBActionPerformed(evt);
            }
        });

        jLabel1.setText("               Calculadora!!!");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputFTF, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(addB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sustractB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(divideB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(multiplyB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sinB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cosB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(logB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(expB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(resultB)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(147, 147, 147))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(inputFTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addB)
                    .addComponent(sustractB)
                    .addComponent(divideB)
                    .addComponent(multiplyB)
                    .addComponent(resultB)
                    .addComponent(sinB)
                    .addComponent(cosB)
                    .addComponent(logB)
                    .addComponent(expB))
                .addGap(18, 18, 18)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBActionPerformed
        try {
            calculatorController.add(Float.valueOf(inputFTF.getValue().toString()));
            inputFTF.setValue(null);
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error: " + Exception.getMessage());
        }
    }//GEN-LAST:event_addBActionPerformed

    private void resultBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resultBActionPerformed
        try {
            float result;
            result = calculatorController.result(Float.valueOf(inputFTF.getValue().toString()));
            inputFTF.setValue(result);
        } catch (NumberFormatException numberFormatException) {
            JOptionPane.showMessageDialog(this, "Operacion no definida");
        } catch (Exception ex){
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage() );
        }
    }//GEN-LAST:event_resultBActionPerformed

    private void sustractBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sustractBActionPerformed
        try {
            calculatorController.sustract(Float.valueOf(inputFTF.getValue().toString()));
            inputFTF.setValue(null);
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_sustractBActionPerformed

    private void multiplyBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_multiplyBActionPerformed
        try {
            calculatorController.multiply(Float.valueOf(inputFTF.getValue().toString()));
            inputFTF.setValue(null);
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_multiplyBActionPerformed

    private void divideBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_divideBActionPerformed
        try {
            calculatorController.divide(Float.valueOf(inputFTF.getValue().toString()));
            inputFTF.setValue(null);
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_divideBActionPerformed

    private void expBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_expBActionPerformed
        try {
            calculatorController.exp(Float.valueOf(inputFTF.getValue().toString()));
            inputFTF.setValue(calculatorController.exp(Float.valueOf(inputFTF.getValue().toString())));
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_expBActionPerformed

    private void logBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logBActionPerformed
        try {
            inputFTF.setValue(calculatorController.log(Float.valueOf(inputFTF.getValue().toString())));
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_logBActionPerformed

    private void cosBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cosBActionPerformed
        try {
            inputFTF.setValue(calculatorController.cos(Float.valueOf(inputFTF.getValue().toString())));
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_cosBActionPerformed

    private void sinBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sinBActionPerformed
        try {
            inputFTF.setValue(calculatorController.sin(Float.valueOf(inputFTF.getValue().toString())));
        } catch (Exception Exception) {
            JOptionPane.showMessageDialog(this, "Error");
        }
    }//GEN-LAST:event_sinBActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PrincipalCalculadora().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addB;
    private javax.swing.JButton cosB;
    private javax.swing.JButton divideB;
    private javax.swing.JButton expB;
    private javax.swing.JFormattedTextField inputFTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton logB;
    private javax.swing.JButton multiplyB;
    private javax.swing.JButton resultB;
    private javax.swing.JButton sinB;
    private javax.swing.JButton sustractB;
    // End of variables declaration//GEN-END:variables

}
